<?php

namespace App\Console\Commands\Shop;

use App\Enums\Common\NsqEnum;
use App\Library\Rms\RmsShopApi;
use App\Models\ChatGroup\ChatGroupModel;
use App\Models\ChatGroup\ChatGroupUserModel;
use App\Service\ChatGroup\ChatGroupMemberService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Kuafu\Nsq\NsqSubscriber;
use NSQClient\Contract\Message;

class NsqSubShopStaffCreateCommand extends Command
{
    protected $signature = 'shop:nsq-sub-shop-staff-event';

    protected $description = 'nsq监听门店员工创建删除事件通知';

    public function handle()
    {
        ini_set('memory_limit', '2G');

        (new NsqSubscriber(config('nsq.default.address')))
            ->sub(
                NsqEnum::TOPIC_RMS_SHOP_STAFF_EVENT, NsqEnum::CHANNEL_TIGER_SHOP_STAFF_EVENT,
                function (Message $message) {
                    $data = $message->data();

                    if (isTest()) {
                        dump($data);
                    }

                    // 执行
                    $this->process($data);

                    $message->done();
                }
            );
    }


    /**
     * AI客服群 人员加入/退出
     * @param array $data
     */
    private function process(array $data): void
    {
        $shopId  = $data['shopId'];
        $userId  = $data['userId'];
        $type    = $data['type'] ?? 0;    // 职位权限类型
        $isExist = $data['isExist'] ?? 0; // 加入/退出

        Log::info('shop-staff-event', ['data' => $data]);

        // 群组是否存在
        $group = ChatGroupModel::query()
            ->where('shopId', $shopId)
            ->where('type', ChatGroupModel::TYPE_CUSTOMER_SERVE)
            ->first();
        if ($group) {
            $isInGroup     = ChatGroupUserModel::inGroup($group->id, $userId);// 是否在群组中
            $isInStaffList = false;                                           // 员工是否有商家职位权限

            // 获得门店下所有员工-只限商家APP的账户
            $staffList = (new RmsShopApi())->getShopStaffListByShopId($shopId, false, false, RmsShopApi::ROLE_BUSINESS_TYPE)['data']['list'] ?? [];
            if ($staffList) {
                $isInStaffList = (bool)collect($staffList)->where('id', $userId)->count();
            }
            Log::info('shop-staff-event-isInGroup', ['data' => $data, 'groupId' => $group->id, 'isInGroup' => $isInGroup, 'isInStaffList' => $isInStaffList]);
            switch ($isExist) {
                case ChatGroupUserModel::IS_EXIST_OFF:
                    // 添加新纪录
                    if (!$isInGroup && $type == RmsShopApi::ROLE_BUSINESS_TYPE && $isInStaffList) {// 仅商家职位可入客服群
                        ChatGroupMemberService::updateExist($shopId, $userId, $isExist, $group->id);
                    }
                    break;
                case ChatGroupUserModel::IS_EXIST_ON:
                    // 修改之前的状态
                    if ($isInGroup && !$isInStaffList) {
                        ChatGroupMemberService::updateExist($shopId, $userId, $isExist, $group->id);
                    }
                    break;
                default:
            }
        }


    }
}
